package com.itheima.springbootwebtlias.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.springbootwebtlias.mapper.StuMapper;
import com.itheima.springbootwebtlias.pojo.PageResult;
import com.itheima.springbootwebtlias.pojo.StuQueryParam;
import com.itheima.springbootwebtlias.pojo.Student;
import com.itheima.springbootwebtlias.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class StuServiceImpl implements StuService {

    @Autowired
    private StuMapper stuMapper;

    // 分页 + 条件查询学员
    @Override
    public PageResult page(StuQueryParam queryParam) {
        PageHelper.startPage(queryParam.getPage(), queryParam.getPageSize());
        Page<Student> page = (Page<Student>) stuMapper.selectByCondition(queryParam);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());

        return pageResult;
    }

    //  添加学员
    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        log.info("添加学员，请求参数：{}", student);
        stuMapper.insert(student);
        log.info("添加学员，结果：{}", student.getId());
    }

    // 批量删除学员
    @Override
    public void removeBatch(List<Integer> ids) {
        log.info("批量删除学员，请求参数：{}", ids);
        stuMapper.deleteBatch(ids);
        log.info("批量删除学员，结果：{}", ids);
    }

    // 根据id查询学员
    @Override
    public Student queryById(Integer id) {
        log.info("根据id查询学员，请求参数：{}", id);
        Student student = stuMapper.selectById(id);
        return student;
    }

    //  修改学员
    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        log.info("修改学员，请求参数：{}", student);
        stuMapper.update(student);
    }

    // 学员违纪处理
    @Override
    public void violation(Integer id, Integer score) {

        log.info("违纪处理，请求参数：{},{}", id, score);
        stuMapper.violation(id, score);
    }
}
